<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { FleetAdd, FleetEdit, FleetShow } from "@/http/api";

export default {
  components: { outMain },
  name: "",
  data() {
    const checkPhone = (rule, value, callback) => {
      let result = this.$ruleCheck.checkPhone(value);
      if (!result) {
        callback();
      } else {
        callback(new Error(result));
      }
    };
    return {
      detailId: this.$route.query.id,
      formData: {
        fleet_name: "",
        fleet_tpy_name: "",
        fleet_mobile: "",
      },
      rules: {
        fleet_name: [
          {
            required: true,
            message: "请输入船队名称",
            trigger: "blur",
            min: 0,
            max: 20,
          },
        ],
        fleet_tpy_name: [
          {
            required: true,
            message: "请输入船队调配员",
            trigger: "blur",
            min: 0,
            max: 10,
          },
        ],
        fleet_mobile: [
          {
            validator: checkPhone,
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {},
  setup() {},
  methods: {
    backFn() {
      this.$router.back();
    },
    async saveFn() {
      const valid = await this.$refs.ruleForm.validate();
      if (!valid) {
        return;
      }
      const { data } = this.detailId
        ? await FleetEdit(this.formData)
        : await FleetAdd(this.formData);
      if (data.code == 1) {
        ElMessage({
          type: "success",
          message: this.detailId ? "编辑成功" : "新增成功",
        });
        setTimeout(() => {
          this.$router.back();
        }, 1000);
      }
    },
    getData() {
      FleetShow({ fleet_id: this.detailId }).then((res) => {
        if (res.data.code == 1) {
          this.formData = res.data.data;
          this.formData["fleet_id"] = this.detailId;
        }
      });
    },
  },
  mounted() {},
  async created() {
    if (this.detailId) {
      this.getData();
    }
  },
};
</script>
<template>
  <outMain>
    <div class="formCont">
      <div class="formHead">
        <div>{{ detailId ? "编辑" : "新增" }}船队</div>
        <btn value="返回上一级" @ok="backFn"></btn>
      </div>
      <div class="formMain">
        <el-form :rules="rules" :model="formData" ref="ruleForm">
          <div class="flexCont">
            <el-form-item
              label="船队名称"
              class="flexPull"
              label-width="120"
              prop="fleet_name"
            >
              <el-input
                clearable
                placeholder="请输入船队名称"
                v-model="formData.fleet_name"
              />
            </el-form-item>
            <el-form-item
              label="调配员"
              class="flexPull"
              label-width="120"
              prop="fleet_tpy_name"
            >
              <el-input
                clearable
                placeholder="请输入调配员"
                v-model="formData.fleet_tpy_name"
              />
            </el-form-item>
            <el-form-item
              label="手机号"
              class="flexPull"
              label-width="120"
              prop="fleet_mobile"
            >
              <el-input
                clearable
                maxlength="11"
                placeholder="请输入手机号"
                v-model="formData.fleet_mobile"
              />
            </el-form-item>
            <el-form-item
              class="flexPull"
              label="船队介绍"
              label-width="120"
              prop="userName"
            >
              <el-input
                v-model="formData.fleet_introduce"
                maxlength="3000"
                placeholder="请输入船队介绍"
                show-word-limit
                :rows="4"
                type="textarea"
              />
            </el-form-item>
          </div>
          <el-form-item class="flexPull" label=" " label-width="120">
            <btn :value="detailId ? '确认编辑' : '确认新增'" @ok="saveFn"></btn>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
